package com.shujia.flink.window;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.*;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

/**
 * @author SHUJIA
 */
public class Demo1ProcessingWindow {
    public static void main(String[] args) throws Exception {
        /*
         * 实时统计单词的数量，每隔5秒统计一次，统计最近5秒（滚动窗口）
         */

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //读取数据
        DataStream<String> lines = env.socketTextStream("master", 8888);
        //一行转换成多行
        DataStream<Tuple2<String, Integer>> kv = lines
                .flatMap((line, collect) -> {
                    String sep = ",";
                    for (String word : line.split(sep)) {
                        collect.collect(Tuple2.of(word, 1));
                    }
                }, Types.TUPLE(Types.STRING, Types.INT));

        //分组
        KeyedStream<Tuple2<String, Integer>, String> keyBy = kv.keyBy(k -> k.f0);

        //划分窗口，每隔5秒一个窗口
        //TumblingProcessingTimeWindows: 滚动的处理时间窗口
        WindowedStream<Tuple2<String, Integer>, String, TimeWindow> window = keyBy
                .window(TumblingProcessingTimeWindows.of(Time.seconds(5)));

        //统计单词的数量
        DataStream<Tuple2<String, Integer>> count = window.sum(1);

        count.print();

        env.execute();
    }
}
